<template>
  <div class="common-layout">
    <el-form class="header" :model="queryParam" ref="queryRef" :inline="true">

      <el-form-item label="内容" prop="ipaddr">
        <el-cascader style="width: 240px" filterable clearable v-model="queryParam.studyContent" :options="bookAndUnitList" @change="handleChange">
          <template #default="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template>
        </el-cascader>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleRecordData">筛选</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <el-col :sm="24" :lg="24" style="padding-left: 20px;padding-right: 20px">
        <el-table border :data="wordTestList">
          <el-table-column label="书名" width="120" align="center" prop="book"/>
          <el-table-column label="单元" width="120" align="center" prop="unit"/>
          <el-table-column label="总数" align="center" prop="wordNum"/>
          <el-table-column label="完成个数" align="center" prop="complete"/>
          <el-table-column label="正确个数" align="center" prop="correctNum"/>
          <el-table-column label="正确率" align="center" prop="correctRate">
            <template #default="scope">
              <span>{{ parseInt(scope.row.correctRate * 100).toFixed(0) }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="测试时间" width="180" align="center" prop="createTime"/>
          <el-table-column label="用时" align="center" prop="usedTime"/>
          <el-table-column label="详情" align="center" width="80" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link @click="getDetails(scope.row)" type="primary">查看</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            style="margin-right: 20px"
            v-show="size > 0"
            :total="size"
            v-model:page="queryParam.pageNum"
            v-model:limit="queryParam.pageSize"
            @pagination="handleRecordData"
        />

      </el-col>
    </el-row>

    <el-dialog style="padding: 20px" title="记录详情" v-model="testResultWindow" width="900px" append-to-body>
      <el-row type="flex" justify="center">
        <el-text style="font-size: 18px;color: rebeccapurple">
          本次测试个数:{{ testCompleteNum }}---正确个数:{{ correctNum }} ---正确率:{{ testCompleteRate }}%
        </el-text>
      </el-row>
      <el-table :data="testCompleteData">
        <el-table-column label="单词/词组" align="center" prop="name"/>
        <el-table-column label="词性" align="center" prop="type"/>
        <el-table-column label="中文名" align="center" prop="chinese"/>
        <el-table-column label="拼写记录" align="center" prop="tryRecord"/>
        <el-table-column label="测试结果" width="100" align="center" prop="result">
          <template #default="scope">
            <dict-tag :options="word_test_status" :value="scope.row.result"/>
          </template>
        </el-table-column>
        <el-table-column label="记录时间" width="180" align="center" prop="time"/>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
// 查询
import {getCurrentInstance, ref} from 'vue';
import {list_write_record} from "@/api/word/wordRecord.js";
import useUserStore from '@/store/modules/user'
import {bookAndUnitList, getWriteRecord} from "@/api/primary/index.js";

const {proxy} = getCurrentInstance();
const {word_test_status} = proxy.useDict("word_test_status")

// 弹窗
let correctNum = ref(0)
let testTotal = ref(0)
let testCompleteNum = ref(0)
let testCompleteData = ref([])
let testCompleteRate = ref(0)
let testResultWindow = ref(false)

let queryParam = ref({
  userId: useUserStore().id,
  type: 'primary_write',
  unit: '',
  book: '',
  pageNum: 1,
  pageSize: 10
})

let wordTestList = ref([])
let groupNum = ref()
let size = ref(0)

initPage()


//连接MYSQL数据库-查询全部
function handleRecordData() {
  // let queryForm = {'userId':useUserStore().id}
  // console.log(queryParams.value)
  list_write_record(queryParam.value).then(res => {
    if (res.code === 200) {
      wordTestList.value = res.rows;
      size.value = res.total;
      // console.log(res)
    }
  })
}

function handleChange(val) {
  // console.log(val)
  queryParam.value.book = val[0]
  queryParam.value.unit = val[1]
  // console.log(queryParam.value)
  handleRecordData()
}

function getDetails(row) {
  // console.log(row)
  if (row.practice === undefined || row.practice === null) {
    proxy.$modal.msgWarning("此记录详情未开放")
    return
  }
  correctNum.value = row.correctNum
  testCompleteNum.value = row.complete
  testCompleteRate.value = parseInt(row.correctRate * 100)
  let query = {
    practice: row.practice,
  }
  getWriteRecord(query).then((res) => {
    if (res.code === 200) {
      // console.log(res)
      testResultWindow.value = true
      testCompleteData.value = res.data
      testTotal.value = res.data.length
    }
  })
}

function initPage() {
  list_write_record(queryParam.value).then(res => {
    if (res.code === 200) {
      wordTestList.value = res.rows;
      size.value = res.total;
      // console.log(res)
    }
  })
}

function resetQuery() {
  queryParam.value.studyContent = undefined
  queryParam.value.unit = undefined
  queryParam.value.book = undefined
  initPage()
}

</script>

<style scoped lang="scss">
.header {
  margin-top: 20px;
  margin-left: 20px;
}
</style>
